// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司 
// SPDX-License-Identifier: GPL-3.0-only 

import type { NodeContent } from "$lib/api/board/board_draw_type";
import { drawStore } from "../store.svelte";
import { get_issue as atomgit_get_issue } from "$lib/api/git_provider/atomgit/issue";
import { get_issue as gitcode_get_issue } from "$lib/api/git_provider/gitcode/issue";
import { get_user_session } from "$lib/api/user";
import { request } from "$lib/utils/request";
import { get as get_extdata, GIT_SERV_ATOMGIT, GIT_SERV_GITCODE } from "$lib/api/board/board_extdata";

export function getNodeType(content: NodeContent): string {
    if (content.UnkwownContent != undefined) {
        return "UnkwownNode";
    } else if (content.TextContent != undefined) {
        return "TextNode";
    } else if (content.BasicRectangleContent != undefined) {
        return "BasicRectangleNode";
    } else if (content.BasicEllipseContent != undefined) {
        return "BasicEllipseNode"
    } else if (content.BasicSvgContent != undefined) {
        return "BasicSvgNode";
    } else if (content.BasicImageContent != undefined) {
        return "BasicImageNode";
    } else if (content.BasicLinkContent != undefined) {
        return "BasicLinkNode";
    } else if (content.LlmAskContent != undefined) {
        return "LlmAskNode";
    } else if (content.LlmAnswerContent != undefined) {
        return "LlmAnswerNode";
    } else if (content.DevelopIssueContent != undefined) {
        return "DevelopIssueNode";
    }

    return "UnkwownNode";
}

//获取提示词
export async function getPromptText(content: NodeContent, handle: string): Promise<string> {
    if (content.TextContent != undefined) {
        return content.TextContent.text;
    } else if (content.BasicRectangleContent != undefined) {
        return content.BasicRectangleContent.text;
    } else if (content.BasicEllipseContent != undefined) {
        return content.BasicEllipseContent.text;
    } else if (content.DevelopIssueContent != undefined) {
        let issueTitle = "";
        let issueContent = "";
        const sessionId = await get_user_session();
        const gitServRes = await request(
            get_extdata({
                session_id: sessionId,
                board_id: drawStore.boardInfo?.board_id ?? "",
                ext_data_id:
                    content.DevelopIssueContent?.git_serv_id ?? "",
            }),
        );
        if (gitServRes.ext_data.basic_info.serv_type.GitServType == GIT_SERV_ATOMGIT) {
            const issueRes = await atomgit_get_issue(
                gitServRes.ext_data.basic_info.access_token,
                content.DevelopIssueContent?.owner_path ?? "",
                content.DevelopIssueContent?.repo_path ?? "",
                content.DevelopIssueContent?.issue_number ?? "",
            );
            issueTitle = issueRes.title ?? "";
            issueContent = issueRes.body ?? "";
        } else if (
            gitServRes.ext_data.basic_info.serv_type.GitServType == GIT_SERV_GITCODE
        ) {
            const issueRes = await gitcode_get_issue(
                gitServRes.ext_data.basic_info.access_token,
                content.DevelopIssueContent?.owner_path ?? "",
                content.DevelopIssueContent?.repo_path ?? "",
                content.DevelopIssueContent?.issue_number ?? "",
            );
            issueTitle = issueRes.title ?? "";
            issueContent = issueRes.body ?? "";
        }
        const tmpList = [`议题标题:${issueTitle}`];
        if (issueContent != "") {
            tmpList.push("议题内容:");
            tmpList.push(issueContent);
        }
        return tmpList.join("\n");
    }
    return "";
}